package com.example.echoewish.ui.main_activity;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.echoewish.econstant.Constant;
import com.example.echoewish.pojo.PartPost;
import com.example.echoewish.repository.PostMessageRepository;
import com.example.echoewish.util.DataGenerator;
import com.example.echoewish.util.LogUtil;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeViewModel extends ViewModel {
    public final static int PART_POST_NUM=5;

    MutableLiveData<List<PartPost>> partPostMutableLiveData;
    MutableLiveData<List<PartPost>> newPartPostMutableLiveData;
    MutableLiveData<String> keywordMutableLiveData;
    MutableLiveData<String> debugkeywordMutableLiveData;
    MutableLiveData<Integer> zoneMutableLiveData;
    MutableLiveData<Integer> startMutableLiveData;
    MutableLiveData<Integer> debugMutableLiveData;

    public HomeViewModel()
    {
        partPostMutableLiveData=new MutableLiveData<>();
        newPartPostMutableLiveData=new MutableLiveData<>();
        keywordMutableLiveData=new MutableLiveData<>();
        debugkeywordMutableLiveData=new MutableLiveData<>();
        zoneMutableLiveData=new MutableLiveData<>();
        startMutableLiveData=new MutableLiveData<>();
        debugMutableLiveData=new MutableLiveData<>();

        partPostMutableLiveData.setValue(new LinkedList<>());
        debugkeywordMutableLiveData.setValue("");
        zoneMutableLiveData.setValue(0);
        debugMutableLiveData.setValue(-1);
    }

    public void getPartPostMessage()
    {
        int zone=zoneMutableLiveData.getValue();
        if(zone==0)
            PostMessageRepository.getInstance().queryPartPost(startMutableLiveData.getValue(),startMutableLiveData.getValue()+PART_POST_NUM,new getPartPostCallBack());
        else
            PostMessageRepository.getInstance().queryPartPostByZone(startMutableLiveData.getValue(),startMutableLiveData.getValue()+PART_POST_NUM, DataGenerator.myTopTab[zone],new getPartPostCallBack());
    }

    public void getPartPostMessageByKeyWord()
    {
        int zone=zoneMutableLiveData.getValue();
        if(zone==0)
            PostMessageRepository.getInstance().queryPartPostByKeyWord(keywordMutableLiveData.getValue(),new getPartPostCallBack());
        else if(zone!=0)
            PostMessageRepository.getInstance().queryPartPostByZoneAndKeyWord(DataGenerator.myTopTab[zone],keywordMutableLiveData.getValue(),new getPartPostCallBack());
    }

    private class getPartPostCallBack implements Callback<List<PartPost>>
    {

        @Override
        public void onResponse(Call<List<PartPost>> call, Response<List<PartPost>> response) {
            newPartPostMutableLiveData.postValue(response.body());
            new LogUtil().i(LogUtil.CALLBACK_TAG,"获取帖子部分消息 回调成功");
        }

        @Override
        public void onFailure(Call<List<PartPost>> call, Throwable t) {
            newPartPostMutableLiveData.postValue(null);
            new LogUtil().w(LogUtil.CALLBACK_TAG,"获取帖子部分消息 回调失败"+t);
        }
    }
}
